package com.app.utils.parallaxviewpager;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;


/**
 * Created by desmond on 1/6/15.
 */
public abstract class ListViewFragment extends ScrollTabHolderFragment {
    protected static final String ARG_POSITION = "position";

    protected ListView mListView;
    protected int mPosition;

    private int topPosition = 1;
    protected boolean isAttch;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        isAttch = true;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        isAttch = false;
    }


    @Override
    public void adjustScroll(int scrollHeight, int headerHeight) {
        if (mListView == null) return;
        if (scrollHeight == 0 && mListView.getFirstVisiblePosition() >= 1) {
            return;
        }
        ParallaxViewPagerBaseActivity activity = (ParallaxViewPagerBaseActivity) this.getActivity();

        if(activity.getScrollYOfListView(mListView) <= 0){
            mListView.setSelectionFromTop(topPosition, scrollHeight);

        }
        activity.scrollHeader(activity.getScrollYOfListView(mListView));

    }




    protected void setListViewOnScrollListener() {
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                onLastViewScroll(view, scrollState);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem > 0) {
                    topPosition = firstVisibleItem;
                }

                if (mScrollTabHolder != null) {
                    mScrollTabHolder.onListViewScroll(
                            view, firstVisibleItem, visibleItemCount, totalItemCount, mPosition);

                }
            }

        });
    }


    protected abstract void onLastViewScroll(AbsListView view, int scrollState);







}